package io.gitee.zhucan123.core.infrastructure.mvc;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;

/**
 * @author: zhuCan
 * @date: 2019/11/18 18:24
 * @description:
 */
@Component
@Slf4j
@RefreshScope
public class BaseUrlConfiguration {

  @Value("${image-server.base-url}")
  private String imgPath;

  @PostConstruct
  public void setup() {
    log.info("baseUrl:-> {}", imgPath);
  }


  public String encodeImgPath(String path) {
    if (!StringUtils.isEmpty(path)) {
      return path.substring(imgPath.length());
    }
    return path;
  }

  public String decodeImgPath(String path) {
    return imgPath + path;
  }

  /**
   * 处理 原始单中的图片路径问题,如果是http开头就不追究basePath,反之就加
   * http://1aswedssfsdfsdf.png
   * /upload/original/sdfsadfsa.png
   *
   * @return
   */
  public String regExpRelativePath(String path) {
    return path.matches("http[s]?://([\\w.]+/?)\\S*") ? path : decodeImgPath(path);
  }

}
